﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIT_CV_Management
{
    public partial class UserControlHocHam : UserControl
    {
        bool isEveryThingOK = false;
        bool isCoQuanCapOK = false;
        bool isQuyetDinhOK = false;

        //object 
        Database myDatabase = Database.shareDatabase;
        HocHam hocHam;
        DaoTaoHocHam daoTaoHocHam;

        public UserControlHocHam()
        {
            InitializeComponent();

            // Hide all 
            this.hideAllErrorImage();

            // init Combobox
            this.initComboBox();

            // init Language
            this.initLanguage();
        }

        private void initLanguage()
        {
            lblTenHocHam.Text = Language._("HOCHAM_FORM_TENHOCHAM_LBL");
            lblNgayCap.Text = Language._("HOCHAM_FORM_NGAYCAP_LBL");
            lblCoQuanCap.Text = Language._("HOCHAM_FORM_COQUANCAP_LBL");
            lblQuyetDinhPhong.Text = Language._("HOCHAM_FORM_QUYETDINH_LBL");
        }

        private void initComboBox()
        {
            // load HocHam for combobox
            QueryResult result = myDatabase.Query("SELECT * FROM HOCHAM");
            if (result.ReturnCode == 200)
            {
                cbxTenHocHam.DataSource = result.Table;
                cbxTenHocHam.DisplayMember = "TenHocHam";
                cbxTenHocHam.ValueMember = "MaTenHocHam";
                cbxTenHocHam.SelectedIndex = 0;
            }
        }

        private void hideAllErrorImage()
        {
            pictureBoxCoQuanCap.Visible = false;
            pictureBoxQuyetDinh.Visible = false;
        }

        private void txbCoQuanCap_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbCoQuanCap.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isCoQuanCapOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbCoQuanCap.BackColor = Color.Tomato;
                utpTip.Value = Language._("HOCHAM_FORM_TIP_COQUANCAP_NULL");
                utpTip.Visible = true;
                pictureBoxCoQuanCap.Visible = false;
            }
            else
            {
                isCoQuanCapOK = true;
                txbCoQuanCap.BackColor = Color.White;
                pictureBoxCoQuanCap.Visible = true;
                utpTip.Visible = false;
            }
        }

        private void txbQuyetDinh_Validating(object sender, CancelEventArgs e)
        {
            bool isHasError = false;

            if (txbQuyetDinh.Text.Trim() == "")
                isHasError = true;

            if (isHasError)
            {
                isQuyetDinhOK = false;
                
                // change NoiSinh's background color to Tomato
                // Add UserTip
                txbQuyetDinh.BackColor = Color.Tomato;
                utpTip.Value = Language._("HOCHAM_FORM_TIP_QUYETDINH_NULL");
                utpTip.Visible = true;
                pictureBoxQuyetDinh.Visible = false;
            }
            else
            {
                isQuyetDinhOK = true;
                txbQuyetDinh.BackColor = Color.White;
                pictureBoxQuyetDinh.Visible = true;
                utpTip.Visible = false;
            }
        }

        public bool isHocHamOK()
        {
            //Validate
            this.txbCoQuanCap_Validating(txbCoQuanCap, null);
            txbQuyetDinh_Validating(txbQuyetDinh, null);

            isEveryThingOK = isQuyetDinhOK && isCoQuanCapOK;
            return isEveryThingOK;
        }
        public DaoTaoHocHam getDaoTaoHocHam()
        {
            if (this.isHocHamOK())
            {
                hocHam = new HocHam(cbxTenHocHam.SelectedValue.ToString());
                daoTaoHocHam = new DaoTaoHocHam(hocHam, dtpNgayCap.Value, txbCoQuanCap.Text, 
                    txbQuyetDinh.Text);
            }
            return daoTaoHocHam;
        }
    }
}
